package com.baosight.sczx.zm.ci.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiBlockMeta;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ef.ui.tree.TreeConstants;
import com.baosight.iplat4j.ef.ui.tree.TreeService;
import com.baosight.iplat4j.ep.QueryMap;
import com.baosight.sczx.zm.domain.ZM02;

/**
 * 
 * <p>Title: ServiceZMCI30.java</p>
 * <p>Description: 生产运行监测</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 郭淑芳
 * @version 1.0
 * <pre>Histroy:
 *          2016年4月8日 下午2:07:28  郭淑芳  Create
 *</pre>
 */
public class ServiceZMCI30 extends TreeService  {

	/**
	 * 
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCI30</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年4月8日下午2:07:14 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
//
//		EiInfo outInfo = super.query(inInfo, "ZM02.query", new ZM02());
//		return outInfo;
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的
		inInfo.addBlock(getMines());
		return inInfo;
	}
	
	

	/**
	 * 矿井模型树操作 
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCI30</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年4月8日下午2:06:31 郭淑芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo query(EiInfo inInfo) {
		QueryMap queryInfo = QueryMap.getQueryMap(inInfo);
		String _node = (String) queryInfo.get(TreeConstants.TREE_NODE);
		List _list = new ArrayList();

		if (_node == null) {
			_node = "$";
		}
		if ("$".equals(_node)) {
			_list = this.getTopNodes();// 得到根节点
		} else {
			_list = this.getChildNodes(_node);
		}

		EiInfo outInfo = new EiInfo();
		EiBlock block = outInfo.addBlock(_node);
		block.setBlockMeta(initMetaData());
		block.setRows(_list);
	//	outInfo.addBlock(getMines());
		return outInfo;
	}
	
	/**
	 * 矿井block
	 * @return
	 */
	private EiBlock getMines() {
		// TODO Auto-generated method stub
		EiBlock block = new EiBlock("cmes.ch.mine");

		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);

		List<Map> _listData = new ArrayList<Map>();
		try {
			_listData = dao.query("ZMCGTsPg.queryTreeChild", null);
		} catch (Exception e) {}
		
		for(int i = 0; i < _listData.size(); i++) {
			_list.add(_listData.get(i)); 
		} 
		block.setRows(_list);
		return block;
	}


	/**
	 * 构造矿井信息树节点的孩子节点
	 * <p>Function: getChildNodes</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCI30</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年4月8日下午2:06:15 郭淑芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	public List getChildNodes(String parentLabel) {
		Map<String, String> _argMap = new HashMap<String, String>();
		_argMap.put(TreeConstants.TREE_NODE, parentLabel);
	
//		String orgCode = UserSession.getInSessionProperty("miningCode").toString();
//		String orgCode = getMineCode();
//		if (!orgCode.equals("defaultOrg")) {
//			_argMap.put("orgCode", orgCode);
//		}
		List<Map> _list = dao.query("ZM02.queryTreeChild", _argMap);
		return _list;
	}
	
	
	/**
	 * 获取根节点
	 * <p>Function: getTopNodes</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCI30</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年4月8日下午2:06:03 郭淑芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	@Override
	public List getTopNodes() {
		List _topNodes = new ArrayList();
		Map<String, String> _argMap = new HashMap<String, String>();
		_topNodes = dao.query("ZM02.queryTreeTop", _argMap);
		return _topNodes;
	}
	
	/**
	 * 叶子列定义
	 * <p>Function: initMetaData</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCI30</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年4月8日下午2:05:53 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiBlockMeta initMetaData() {
		EiBlockMeta eiMetadata = new EiBlockMeta();

		EiColumn eiColumn = new EiColumn("text");
		eiColumn.setDescName("显示名称");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("label");
		eiColumn.setDescName("实际值");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
//		imagePath
		eiColumn = new EiColumn("level");
		eiColumn.setDescName("树级别");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("type");
		eiColumn.setDescName("所属类别");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("leaf");
		eiColumn.setDescName("是否叶子节点");
		eiColumn.setType(EiConstant.COLUMN_TYPE_NUMBER);
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		
		return eiMetadata;
	}

	/**
	 * 获取登陆用户所在的矿名
	 * <p>Function: getMineCode</p>
	 * <p>Description: </p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年4月8日下午1:46:40 郭淑芳
	 * </p>
	 *
	 */
	private String getMineCode() {
		// 获取当前登陆用户所在组织机构id
		String orgCode = UserSession.getInSessionProperty("orgCode").toString().trim();

		List<ZM02> mineList = new ArrayList();
		Map<String, String> _argMap = new HashMap<String, String>();
		_argMap.put("orgCode", orgCode);
		mineList = dao.query("ZM02.query", _argMap);

		String parentName = mineList.get(0).getParentName();
		orgCode = parentName;
		while (!parentName.equals("sczx") && !parentName.equals("defaultOrg")) {
			_argMap.put("orgCode", parentName);
			mineList = dao.query("ZM02.query", _argMap);

			parentName = mineList.get(0).getParentName(); // 获取父节点
			orgCode = mineList.get(0).getOrgCode();
		}

		return orgCode;
	}
}
